package com.etop.weibo.dao;

import com.etop.weibo.beans.Praise;
import com.etop.weibo.util.HibernateUtil;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.Session;

/**
 *
 * @author jansing lz_jansing@163.com
 */
public class PraiseDao extends BaseDao<Praise>{
    private final static String ID="id";
    private final static String SAY_ID="sayId";
    private final static String COMMENT_ID="commentId";
    private final static String USER_ID="userId";
    
    @Override
    public void add(Praise praise) {
        super.add(praise);
    }

    /**
     * sayId与commentId至少有一个为0
     *
     * @param userId
     * @param sayId
     * @param commentId
     */
    public void delete(int userId, int sayId, int commentId) {
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("sayId", sayId);
        params.put("commentId", commentId);
//        super.deleteByHQL(Praise.class, params);
    }

    public Praise load(int userId, int sayId, int commentId) {
        Session session = HibernateUtil.openSession();
        Praise p = (Praise) session.createQuery("from Praise p where p.sayId=:sayId and p.commentId=:commentId and p.userId=:userId")
                .setInteger("userId", userId)
                .setInteger("sayId", sayId)
                .setInteger("commentId", commentId)
                .uniqueResult();
        session.close();
        return p;
    }

    /**
     * sayId与commentId至少有一个为0
     *
     * @param sayId
     * @param commentId
     * @return
     */
    public int count(int sayId, int commentId) {
        Session session = HibernateUtil.openSession();
        Number count = (Number) session.createQuery("select count(*) from Praise p where p.sayId=:sayId and p.commentId=:commentId")
                .setInteger("sayId", sayId)
                .setInteger("commentId", commentId)
                .uniqueResult();
        session.close();
        return count.intValue();
    }
}
